(function(){
	var TRANSACTIONID = new Date().getTime();
	var REQUEST_TIMEOUT = 10000;
	var SERVER = window.location.protocol + '//' +window.location.host+ '/json/Data/';
	var DEV_SERVER = 'http://211.239.159.41:10000/json/Data/';
	var REQUEST_DATA = {
		'CHANNELLIST' : {url: 'ChannelList.asp', method: 'get'},
		'PROGRAMLIST' : {url: 'ProgramList.asp', method: 'get'},
		'EPISODELIST' : {url: 'EpisodeList.asp', method: 'get'},
		'EPISODESTREAMINGURL' : {url: 'EpiSodeStreamingUrl.asp', method: 'get'},
		'PROGRAMCONTENTSLIST' : {url: 'ProgramContentsList.asp', method: 'get'},
		'EPISODELIKE' : {url: 'EpisodeLike.asp', method: 'get'},
		'EPISODESTATICS' : {url: 'EpisodeStatics.asp', method: 'get'},
		'PLAYLIST' : {url: 'PlayList.asp', method: 'get'},
		'PAYMENTHISTORY' : {url: 'PaymentHistory.asp', method: 'get'},
		'NOTICE' : {url: 'Notice.asp', method: 'get'},
		'BANNERLIST' : {url: 'BannerList.asp', method: 'get'},
		'EVENTLIST' : {url: 'EventList.asp', method: 'get'},
		'EVENTINSERT' : {url: 'EventInsert.asp', method: 'get'},
		'EVENTDETAIL' : {url: 'EventDetail.asp', method: 'get'},
		'POLL' : {url: 'Poll.asp', method: 'get'},
		'POLLINSERT' : {url: 'PollInsert.asp', method: 'get'},
		'CHANNELING' : {url: 'Channeling.asp', method: 'get'},
		'SEARCH' : {url: 'Search.asp', method: 'get'},
		'BUY' : {url: 'Buy.asp', method: 'get'},
		'CONNECTIONLOGINSERT' : {url: 'ConnectionLogInsert.asp', method: 'get'},
		'RETURNCHANNELCODE' : {url: 'ReturnChannelCode.asp', method: 'get'}
	};

	var RESPONSE_DATA = {};
	var xhr;
	//var preURL;
	function Ajax (apiObj) {
		// if (xhr && xhr.readystate != 4) {
			// console.log('http ajax abort');
			// xhr.abort();
		// }
		var target_url, param_data;
		if (CJ.IS_TV) {
			target_url = SERVER + apiObj.url;
			param_data = apiObj.param;
		} else {
			target_url = 'proxy.asp?url=' + encodeURIComponent(DEV_SERVER + apiObj.url+ "?"+ $.param(apiObj.param));
			param_data = '';
		}
		xhr = $.ajax({
			type: apiObj.method,
			url: target_url,
			data: param_data,
			timeout: REQUEST_TIMEOUT,
			dataType: 'json',
			success: function (jsonObj, textStatus, jqXHR) {
				console.log('success :' + apiObj.url);
				if (jsonObj.resultcode == 200 || jsonObj.resultcode == 300) {
					console.log(jsonObj.message + ': ' + apiObj.url);
				}
				if (typeof apiObj.success == 'function') {
					apiObj.success(jsonObj);
				}
			},
			error:  function (jqXHR, textStatus, errorThrown) {
				console.log('error: ' + apiObj.url);
				if (typeof apiObj.error == 'function') {
					apiObj.error();
				}
				//if (typeof reqObj.errorCallBack == 'function') { reqObj.complete(jqXHR, textStatus); }
			},
			complete: function (jqXHR, textStatus) {
				if (typeof apiObj.complete == 'function') {
					apiObj.complete();
				}
				//if (typeof reqObj.complete == 'function') { reqObj.complete(jqXHR, textStatus); }
			}
		});
	}

	function options (obj){
		var preObj = REQUEST_DATA[obj.type];
		for( var p in preObj) {
			obj[p] = preObj[p];
		}
		if (!('param' in obj)) {
			obj.param = {};
		}
		obj.param.transactionId = TRANSACTIONID;
		return obj;
	}


	var HttpJson = {
		get: function(obj) {
			var newOption = options(obj);
			Ajax(newOption);
		},
		set: function(type) {
			var opt = options(type);
			Ajax(opt);
		}
	};
	if (!window.CJ) {
		window.CJ = {};
	}
	//window.CJ.REQUEST_PARAM = REQUEST_PARAM;
	window.CJ.HttpJson = HttpJson;
})();